package cn.com.dcsgo.controller;

import cn.com.dcsgo.common.Result;
import cn.com.dcsgo.domain.dto.SysMenuSaveDTO;
import cn.com.dcsgo.domain.dto.SysMenuUpdateDTO;
import cn.com.dcsgo.domain.po.SysMenu;
import cn.com.dcsgo.domain.vo.RouterVO;
import cn.com.dcsgo.domain.vo.SysMenuVO;
import cn.com.dcsgo.permission.MyPermission;
import cn.com.dcsgo.service.SysMenuService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.Serializable;
import java.util.List;

/**
 * SysMenuController
 *
 * @author Dcsgo
 * @since 28/5/2025 上午 8:39
 */
@Tag(name = "菜单")
@RestController
@RequestMapping("sysMenu")
@RequiredArgsConstructor
public class SysMenuController extends BaseController {
    private final SysMenuService sysMenuService;

    @Operation(summary = "获取动态路由")
    @GetMapping("getRouters")
    public Result<List<RouterVO>> getRouters() {
        List<RouterVO> routerVOList = sysMenuService.getRouters();
        return new Result<>(routerVOList);
    }

    @MyPermission({"system:menu:query"})
    @Operation(summary = "获取所有菜单", description = "用户拥有其权限的所有菜单，返回的是层级结构的菜单列表")
    @GetMapping("listMenu")
    public Result<List<SysMenuVO>> getAll() {
        List<SysMenuVO> menuList = sysMenuService.list();
        return new Result<>(menuList);
    }

    @MyPermission({"system:menu:query"})
    @Operation(summary = "仅获取所有目录和菜单", description = "用户拥有其权限的所有目录和菜单，返回的是层级结构的菜单列表")
    @GetMapping("listMenuMC")
    public Result<List<SysMenuVO>> getAllDirectoryAndMenu() {
        List<SysMenuVO> directoryAndMenuList = sysMenuService.directoryAndMenuList();
        return new Result<>(directoryAndMenuList);
    }

    @MyPermission({"system:menu:query"})
    @Operation(summary = "获取菜单信息", description = "根据菜单编号")
    @GetMapping("get/{menuId}")
    public Result<SysMenu> getList(@PathVariable Long menuId) {
        SysMenu sysMenu = sysMenuService.get(menuId);
        return new Result<>(sysMenu);
    }

    @MyPermission({"system:menu:add"})
    @Operation(summary = "添加菜单")
    @PostMapping("save")
    public Result<Serializable> save(@Validated @RequestBody SysMenuSaveDTO saveDTO) {
        int row = sysMenuService.save(saveDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:menu:update"})
    @Operation(summary = "修改菜单", description = "不能更改菜单类型、父节点编号")
    @PutMapping("update")
    public Result<Serializable> update(@Validated @RequestBody SysMenuUpdateDTO updateDTO) {
        int row = sysMenuService.update(updateDTO);
        return chooseReturn(row > 0);
    }

    @MyPermission({"system:menu:remove"})
    @Operation(summary = "删除菜单", description = "根据菜单编号，只允许删除没有子元素的菜单")
    @DeleteMapping("delete/{menuId}")
    public Result<Serializable> delete(@PathVariable Long menuId) {
        int row = sysMenuService.remove(menuId);
        return chooseReturn(row > 0);
    }
}
